package com.okex.api.examples;

import java.util.Map;

import com.okex.api.client.OkexApiClientFactory;
import com.okex.api.client.OkexApiWebSocketClient;
import com.okex.api.client.domain.general.DepthLimit;
import com.okex.api.client.domain.market.AggTrade;

/**
 * Illustrates how to use the aggTrades event stream to create a local cache of trades for a symbol.
 */
public class AggTradesCacheExample {

  /**
   * Key is the aggregate trade id, and the value contains the aggregated trade data, which is
   * automatically updated whenever a new agg data stream event arrives.
   */
  private Map<Long, AggTrade> aggTradesCache;

  public AggTradesCacheExample(String symbol) {
    startAggTradesEventStreaming(symbol);
  }


  /**
   * Begins streaming of agg trades events.
   */
  private void startAggTradesEventStreaming(String symbol) {
    OkexApiClientFactory factory = OkexApiClientFactory.newInstance();
    OkexApiWebSocketClient client = factory.newWebSocketClient();

    client.onOkSubSpotDepth(symbol.toLowerCase(),DepthLimit._10,response->{
    	System.out.println(response[0].toString());
    });
  }

  /**
   * @return an aggTrades cache, containing the aggregated trade id as the key,
   * and the agg trade data as the value.
   */
  public Map<Long, AggTrade> getAggTradesCache() {
    return aggTradesCache;
  }

  public static void main(String[] args) {
    new AggTradesCacheExample("ETH_BTC");
  }
}
